package algorithm;

/**
 * Created by laiweijia on 2020/3/23.
 */
public class ByteBusLwj {

    public static int getStart(int[] gas, int[] cost) {
        return put(0, 0, gas, cost, true, -1, 0);
    }

    private static int put(int i, int useless, int[] gas, int[] cost, boolean isFirst, int start, int times) {
        if (gas == null || cost == null || gas.length != cost.length) {
            return -1;
        }
        if (isFirst) {
            times++;
        }
        if (times > gas.length) {
            return -1;
        }
        int a = gas[i] + useless - cost[i];
        if (gas[i] + useless >= cost[i]) {
            if (start == i && !isFirst) {
                return start;
            }
            if (isFirst) {
                start = i;
            }

            return put(i + 1 > gas.length - 1 ? 0 : i + 1, a, gas, cost, false, start, times);
        }
        return put(i + 1 > gas.length - 1 ? 0 : i + 1, 0, gas, cost, true, start, times);
    }

}
